package com.willren;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import com.willren.DefaultAlertDialog.DialogType;
import com.willren.usharejni.UshareJni;

public class AvtServer extends FrameActivity implements WifiManagerEx.WifiApStateInterface, UshareJni.UShareStateInterface {
	
	private DefaultAlertDialog alertDialog;
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.server);
		
		final WifiManagerEx wifi = WifiManagerEx.getWifiManagerExInstance(AvtServer.this);
		final UshareJni ushare = UshareJni.getUShareManagerInstance(AvtServer.this);
		
		Button launchAp = (Button) this.findViewById(R.id.launch_ap);
		launchAp.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (null != wifi) {
					if (wifi.isWifiApStarted()) {
						wifi.stopWifiAp(AvtServer.this);
					}
					else {
						alertDialog = new DefaultAlertDialog(AvtServer.this, DialogType.DialogType_Waitting);
						if (null != alertDialog) {
							alertDialog.setText("Starting Hot Sopt ...", null, null, null, null, null);
							alertDialog.show(true);
						}
						
						wifi.startWifiAp(AvtServer.this);
					}
				}
			}
		});
		
		Button launchServer = (Button) this.findViewById(R.id.launch_server);
		launchServer.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (null != ushare) {
					if (ushare.isUShareStarted()) {
						ushare.stopUShare(AvtServer.this);
					}
					else {
						alertDialog = new DefaultAlertDialog(AvtServer.this, DialogType.DialogType_Waitting);
						if (null != alertDialog) {
							alertDialog.setText("Starting Media Server ...", null, null, null, null, null);
							alertDialog.show(true);
						}
						
						ushare.startUShare("DLNA_Server", "/sdcard/dlna", AvtServer.this);
					}
				}
			}
		});

		Button setting = (Button) this.findViewById(R.id.setting);
		setting.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				startActivity(new Intent(AvtServer.this, AvtSetting.class));
			}
		});
		
		if (null != wifi && false == wifi.isWifiApStarted()) {
			findViewById(R.id.line_launch_ap).setVisibility(View.INVISIBLE);
		}
		findViewById(R.id.line_launch_server).setVisibility(View.INVISIBLE);
		findViewById(R.id.line_launch_viewer).setVisibility(View.INVISIBLE);
	}
	
	@Override
	public void onDestroy() {
		super.onDestroy();
		
		UshareJni.getUShareManagerInstance(AvtServer.this).stopUShare(null);
	}

	@Override
	public void wifiApStateChanged(WifuApStateType state) {
		
		switch (state) {
		case ApState_Started:
			runOnUiThread(new Runnable() {
				public void run() {
					if (null != alertDialog) {
						alertDialog.show(false);
					}
					
					findViewById(R.id.line_launch_ap).setVisibility(View.VISIBLE);
					Toast.makeText(AvtServer.this, "Hot Spot was Started", Toast.LENGTH_SHORT).show();
				}
			});
			break;
		case ApState_Stoped:
			runOnUiThread(new Runnable() {
				public void run() {
					if (null != alertDialog) {
						alertDialog.show(false);
					}
					
					findViewById(R.id.line_launch_ap).setVisibility(View.INVISIBLE);
					Toast.makeText(AvtServer.this, "Hot Spot was Stopped", Toast.LENGTH_SHORT).show();
				}
			});
			break;
		}
	}

	@Override
	public void ushareStateChanged(UShareStateType state) {
		
		switch (state) {
		case UsState_Started:
			runOnUiThread(new Runnable() {
				public void run() {
					if (null != alertDialog) {
						alertDialog.show(false);
					}
					
					findViewById(R.id.line_launch_server).setVisibility(View.VISIBLE);
					Toast.makeText(AvtServer.this, "Media Server was Started", Toast.LENGTH_SHORT).show();
				}
			});
			break;
		case UsState_Stoped:
			runOnUiThread(new Runnable() {
				public void run() {
					if (null != alertDialog) {
						alertDialog.show(false);
					}
					
					findViewById(R.id.line_launch_server).setVisibility(View.INVISIBLE);
					Toast.makeText(AvtServer.this, "Media Server was Stopped", Toast.LENGTH_SHORT).show();
				}
			});
			break;
		case UsState_Error:
			break;
		}
	}
}
